package Experiment3;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Iterator;
import java.util.Scanner;

import edu.princeton.cs.algs4.*;

public class Paths {
    private MyDijkstra m;

    public Paths(EuclidienGraph graph,String request_data){
        m=new MyDijkstra(graph);
        Scanner sc=null;
        try{
            Path path=java.nio.file.Paths.get(".\\src\\Experiment3\\"+request_data);
            sc=new Scanner(path.toFile());

            while(sc.hasNextInt()){
                int source=sc.nextInt();
                int destination=sc.nextInt();
                m.calculateShortPath(source, destination);
                printResults(source, destination);
            }

        }catch(IOException e){
            System.err.println(e);
        }finally{
            sc.close();
        }
    }

    private void printResults(int source,int destination){
        System.out.print("["+source+"->"+destination+"]\t");
        if(!m.hasPath()){
            System.out.println("No Path");
            return;
        }
        
        System.out.printf("DISTANCE:%.2f\tPATH:",m.distance());
        
        Iterator<Edge> it=m.path().iterator();
        int current_node=source;
        while(it.hasNext()){
            System.out.print(current_node);
            System.out.print("->");
            current_node=it.next().other(current_node);
        }
        System.out.println(destination);
    }

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.print("输入地图文件: ");
        String map_data=sc.next();
        System.out.print("输入路径问题文件: ");
        String questions_data=sc.next();
        Paths paths=new Paths(new InputData(map_data).getGraph(), questions_data);
        sc.close();
    }
}
